package com.lelouch.bestpractice.mongorepository.controller;

import com.lelouch.bestpractice.mongorepository.common.ServerResponse;
import com.lelouch.bestpractice.mongorepository.domain.WetlandBasicEntity;
import com.lelouch.bestpractice.mongorepository.param.WetlandBasicEntityParam;
import com.lelouch.bestpractice.mongorepository.service.WetlandBasicEntityServive;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;

/**
 * 保护主体管理
 *
 * @author lvchuanyao
 * @date 2016.8.2
 */

@Controller
@RequestMapping(value = "/wetlandManager")
@Slf4j
public class WetLandManagerController {

    @Autowired
    private WetlandBasicEntityServive wetlandBasicEntityServive;

    @RequestMapping(value = "/indexNew", method = RequestMethod.GET)
    public String indexNew(HttpSession session) {
        // ModelAndView mav = new ModelAndView("wetlandmanager/indexnew");
        return "wetlandmanager/indexnew";
    }


    /**
     * 根据保护主体的id列表查找对应图片地址的列表
     * 按照下面这种格式传递id的话，springmvc会自动将其转化为list
     * http://localhost:8080/wetlandManager/findWetlandPicturesByWetlandIds?wetlandIds=5859f56be4b06344e03cb18c,585a26e4e4b06344e03cb18d
     *
     * @param wetlandIds
     * @return
     */
    @RequestMapping(value = "/findWetlandPicturesByWetlandIds")
    @ResponseBody
    public List<String> findWetlandPicturesByWetlandIds(@RequestParam("wetlandIds") List<String> wetlandIds) {
        log.info("接受到的参数为:{}", wetlandIds);
        return wetlandBasicEntityServive.findWetlandPicturesByWetlandIds(wetlandIds);
    }

    /**
     * 根据保护主体的id查找对应的保护主体实体
     *
     * @param wetlandId
     * @return
     */

    @RequestMapping(value = "/findWetlandByWetlandId")
    @ResponseBody
    public WetlandBasicEntity findWetlandByWetlandId(
            String wetlandId) {
        log.info("接受到的参数为:{}", wetlandId);
        return wetlandBasicEntityServive.findWetlandByWetlandId(wetlandId);
    }

    /**
     * 检查保护主体名是否已经存在
     *
     * @param wetlandName
     * @return
     */
    @RequestMapping(value = "/checkWetlandNameExists", method = RequestMethod.POST)
    @ResponseBody
    public boolean checkWetlandNameExists(@RequestParam("wetlandName") String wetlandName) {
        return wetlandBasicEntityServive.checkWetlandNameExists(wetlandName);
    }

    /**
     * 根据名称查询保护主体
     *
     * @param wetlandName
     * @return
     */
    @RequestMapping(value = "/findWetlandByWetlandName", method = RequestMethod.POST)
    @ResponseBody
    public WetlandBasicEntity findWetlandByWetlandName(@RequestParam("wetlandName") String wetlandName) {
        return wetlandBasicEntityServive.findWetlandByWetlandName(wetlandName);
    }

    /**
     * 多条件查询保护主体列表
     *
     * @return
     */
    @RequestMapping(value = "/findWetlandsByMultiConditions", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<List<WetlandBasicEntity>> findWetlandsByMultiConditions(@RequestParam("queryString") String queryString) {


        return wetlandBasicEntityServive.findWetlandsByMultiConditions(queryString);
    }


    /**
     * 根据level分类，统计各类湿地公园的个数BasicDBObject
     */

    @RequestMapping(value = "/getWetlandCountsByLevel", method = RequestMethod.POST)
    @ResponseBody
    public List<Document> getWetlandCountsByLevel() {
        return wetlandBasicEntityServive.getWetlandCountsByLevel();


    }

    /**
     * 根据level进行分类，统计给类型湿地的面积和
     *
     * @return
     */
    @RequestMapping(value = "/getWetlandAreasByLevel", method = RequestMethod.POST)
    @ResponseBody
    public List<Document> getWetlandAreasByLevel() {
        return wetlandBasicEntityServive.getWetlandAreasByLevel();


    }

    /**
     * 根据前端传来的分页和排序属性返回对应的保护主体列表
     *
     * @param start
     * @param length
     * @param orderName
     * @param order
     * @return
     */
    @RequestMapping(value = "/getAllWetlandsByPageAndSort", method = RequestMethod.POST)
    @ResponseBody
    public List<WetlandBasicEntity> getAllWetlandsByPageAndSort(
            @RequestParam(value = "start", defaultValue = "0") Integer start,
            @RequestParam(value = "length", defaultValue = "10") Integer length,
            @RequestParam(value = "orderName", defaultValue = "wlin_name") String orderName,
            @RequestParam(value = "order", defaultValue = "desc") String order

    ) {
        return wetlandBasicEntityServive.getAllWetlandsByPage(start, length, orderName, order);
    }

    /**
     * 添加湿地公园基本信息
     *
     * @param wetlandBasicEntityParam
     * @return
     */
    @RequestMapping(value = "/addWetlandBasicInfo", method = RequestMethod.POST)
    @ResponseBody
    public WetlandBasicEntity addWetlandBasicInfo(
            //@RequestParam("name") String name,
            //@RequestParam("gismapserver") String gismapserver,
            //@RequestParam("addprov") String prov,
            //@RequestParam("addcity") String city,
            //@RequestParam(value = "adddist", required = false, defaultValue = "") String dist,
            //@RequestParam("latitude") double latitude,
            //@RequestParam("longitude") double longitude,
            //@RequestParam("scale") int scale,
            //@RequestParam("type") String type
            WetlandBasicEntityParam wetlandBasicEntityParam
    ) {
        return wetlandBasicEntityServive.addWetlandBasicInfo(wetlandBasicEntityParam);
    }

    /***
     * 删除湿地
     *
     * @return 0成功，1存在下级，2失败
     */
    @RequestMapping(value = "/deleteWetlandByWetlandId", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse deleteWetlandByWetlandId(@RequestParam("wetlandId") String wetlandId) {
        return wetlandBasicEntityServive.deleteWetlandByWetlandId(wetlandId);

    }


    @RequestMapping(value = "/findAllWetlands", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<List<WetlandBasicEntity>> findAllWetlands() {

        return wetlandBasicEntityServive.findAllWetlands();
    }


    @RequestMapping(value = "/findWetlandsByWetlandIds", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<List<WetlandBasicEntity>> findWetlandsByWetlandIds(@RequestParam("wetlandIds") List<String> wetlandIds) {

        return wetlandBasicEntityServive.findWetlandsByWetlandIds(wetlandIds);
    }

    /**
     * 根据传来的json作为查询条件进行模糊查询
     *
     * @param queryString
     * @return
     */

    @RequestMapping(value = "/findWetlandsByFuzzySearch", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<List<WetlandBasicEntity>> findWetlandsByFuzzySearch(@RequestParam("queryString") String queryString) {
        return wetlandBasicEntityServive.findWetlandsByFuzzySearch(queryString);
    }


    @RequestMapping(value = "/uploadWetlandsExcel", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse uploadWetlandsExcel(@RequestParam("file") MultipartFile uploadExcel) throws IOException {
        return wetlandBasicEntityServive.uploadWetlandsExcel(uploadExcel);
    }

}
